package com.muc.plugin;

import com.muc.plugin.handler.*;
import com.muc.plugin.interceptor.MultiUserChat;
import com.muc.plugin.interceptor.SessionEventListenerImpl;
import org.apache.log4j.BasicConfigurator;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.event.SessionEventDispatcher;
import org.jivesoftware.openfire.event.SessionEventListener;
import org.jivesoftware.openfire.muc.MUCEventDispatcher;
import org.jivesoftware.openfire.muc.MUCEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
 * 插件入口类, 负责注入插件的功能
 */
public class MUCPersistencePlugin implements Plugin {

    private MUCEventListener mucEventListener = new MultiUserChat();

    private SessionEventListener sessionEventListener = new SessionEventListenerImpl();

    private static final Logger log = LoggerFactory.getLogger(MUCPersistencePlugin.class);

    @Override
    public void initializePlugin(PluginManager manager, File pluginDirectory) {
        //自动快速地使用缺省Log4j环境
        BasicConfigurator.configure();
        log.warn("@@@@@@@@@@@@@  群组插件已经启动Join room plugin is running!");
        MUCEventDispatcher.addListener(mucEventListener);
        SessionEventDispatcher.addListener(sessionEventListener);


        XMPPServer.getInstance().getIQRouter().addHandler(new MUCUserJoinedRoomsQueryIQHandler());
        XMPPServer.getInstance().getIQRouter().addHandler(new MUCMemberQueryIQHandler());
        XMPPServer.getInstance().getIQRouter().addHandler(new MUCMemberRemoveIQHandler());
        XMPPServer.getInstance().getIQRouter().addHandler(new MUCUngroupIQHandler());
        XMPPServer.getInstance().getIQRouter().addHandler(new MUCJoinGroupHandler());
        XMPPServer.getInstance().getIQRouter().addHandler(new MUCSetAdminIQHandler());

        /*
        XMPPServer.getInstance().getIQRouter().addHandler(new MUCJoinIQHandler());
        XMPPServer.getInstance().getIQRouter().addHandler(new MUCActiveJoinGroupIQHandler());
        */
    }


    @Override
    public void destroyPlugin() {
        MUCEventDispatcher.removeListener(mucEventListener);
        SessionEventDispatcher.removeListener(sessionEventListener);
    }


}
